package com.mapapp.flutter_mapapp.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.chrisbanes.photoview.PhotoView;
import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.util.GlideApp;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PreviewPictureActivity extends ImmersiveBaseActivity {


    @BindView(R.id.view_pager)
    SlideViewPager viewPager;
    @BindView(R.id.tv_current)
    TextView tvCurrent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_picture);
        ButterKnife.bind(this);

        final ArrayList<String> images = getIntent().getStringArrayListExtra("images");
        int current = getIntent().getIntExtra("current", 1);


        tvCurrent.setText(String.format("%d/%d", current, images.size()));
        final List<PhotoView> imageViews = new ArrayList<>();
        for (int i = 0; i < images.size(); i++) {
            PhotoView imageView = new PhotoView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageViews.add(imageView);
        }

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return images.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view == o;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView(imageViews.get(position));
            }

            @NonNull
            @Override
            public ImageView instantiateItem(@NonNull ViewGroup container, int position) {
                ImageView imageView = imageViews.get(position);
                container.addView(imageView);
                GlideApp.with(mContext).load(images.get(position)).into(imageView);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                return imageView;
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                tvCurrent.setText(position + 1 + "/" + images.size());
            }
        });

        if (current > 0) {
            viewPager.setCurrentItem(current - 1);
        }

    }

    public static class SlideViewPager extends ViewPager {

        public SlideViewPager(Context context) {
            super(context);
        }

        public SlideViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            try {
                return super.onInterceptTouchEvent(ev);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    }
}
